#!/bin/bash

# -------------升级依赖环境---------
updatePrefix(){
			color blue "begin install prefix"			
			yum -y install gcc pam-devel bzip2 wget make > /dev/null 2>&1
			cd $pwd/files
			# 升级zlib
			tar xzf $zlib_version.tar.gz
			cd $pwd/files/$zlib_version
			color blue "begin to update zlib"
			./configure --prefix=$prefix/$zlib_version > /dev/null 2>&1
			make > /dev/null 2>&1
			make install > /dev/null 2>&1
			echo "$prefix/$zlib_version/lib" >> /etc/ld.so.conf
			ldconfig
			if [ -e $prefix/$zlib_version/lib/libz.so ];then
				echo -e "安装软件依赖包成功" "\033[32m Success\033[0m"
			else
				echo -e "安装软件依赖包失败，五秒后自动退出脚本" "\033[31m Failure\033[0m"
				echo ""
				sleep 5
				exit
			fi		
}


# -------安装升级telnet--------
function upgradeTelnet(){			
    color blue "begin install telnet-server"
    color blue "begin yum install telnet-server xinetd"
    yum install -y telnet-server xinetd | grep 完毕
				# 默认情况下，系统是不允许 root 用户 telnet 远程登录的。
				#如果要使用 root 用户直接登录，需设置如下内容:
    echo -e 'pts/0\npts/1\npts/2\npts/3'  >>/etc/securetty
				color blue "start telnet"
    systemctl enable xinetd.service
    systemctl enable telnet.socket
    systemctl start telnet.socket
    systemctl start xinetd
				# 查看telnet是否已经启动
    checkServerPort 23 telnet-server
}

# --------------安装dropbear，监听端口为6666--------
function upgradeDropbear(){
		color blue "begin install dropbear"
		yum -y install bzip2 |grep 完毕 
		# cd $basedir/files
		cd $pwd/files
		tar xjf $dropbear_version.tar.bz2
		cd $dropbear_version
		color blue "begin to make dropbear"
		./configure --disable-zlib > /dev/null 2>&1		
		make > /dev/null 2>&1
		make install > /dev/null 2>&1
		
		color blue "configure dropbear"
		mkdir -p /etc/dropbear
		/usr/local/bin/dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key > /dev/null 2>&1
		/usr/local/bin/dropbearkey -t rsa -s 4096 -f /etc/dropbear/dropbear_rsa_host_key > /dev/null 2>&1
		/usr/local/sbin/dropbear -p 6666 > /dev/null 2>&1
		
		# 查看端口是否启动
		checkServerPort 6666 dropbear
}


# --------------备份之前的openssh---------

function  backupSSH(){
	color blue "create backup dirs"
	#创建备份目录
	mkdir -p /tmp/backup_$date/openssh
	mkdir -p /tmp/backup_$date/openssh/usr/{bin,sbin}
	mkdir -p /tmp/backup_$date/openssh/etc/{init.d,pam.d,ssh}
	mkdir -p /tmp/backup_$date/openssh/usr/libexec/openssh
	mkdir -p /tmp/backup_$date/openssh/usr/share/man/{man1,man8}
	# 将openssh相关信息备份到文件里面
	rpm -ql openssh > /tmp/backup_$date/openssh/openssh-rpm-backup-list.txt
	rpm -ql openssh-server > /tmp/backup_$date/openssh/openssh-server-rpm-backup-list.txt
	find / -name "ssh*" > /tmp/backup_$date/openssh/openssh-backup-list.txt

	openssh_rpm_status=`rpm -qa | grep -w "openssh-server" | wc -l`
	
	color blue "backup ssh files to /tmp "
	# 如果之前已经安装过SSH
	if [ "$openssh_rpm_status" != "0" ];then
				cp /usr/bin/ssh* /tmp/backup_$date/openssh/usr/bin > /dev/null 2>&1
				cp /usr/sbin/sshd /tmp/backup_$date/openssh/usr/sbin > /dev/null 2>&1
				cp /etc/init.d/sshd /tmp/backup_$date/openssh/etc/init.d > /dev/null 2>&1
				cp /etc/pam.d/sshd /tmp/backup_$date/openssh/etc/pam.d > /dev/null 2>&1
				cp /etc/ssh/ssh* /tmp/backup_$date/openssh/etc/ssh > /dev/null 2>&1
				cp /etc/ssh/sshd_config /tmp/backup_$date/openssh/etc/ssh > /dev/null 2>&1
				cp /usr/share/man/man1/ssh* /tmp/backup_$date/openssh/usr/share/man/man1 > /dev/null 2>&1
				cp /usr/share/man/man8/ssh* /tmp/backup_$date/openssh/usr/share/man/man8 > /dev/null 2>&1
				cp /usr/libexec/openssh/ssh* /tmp/backup_$date/openssh/usr/libexec/openssh > /dev/null 2>&1
			# 停止服务
				color blue "stop  sshd"
				service sshd stop > /dev/null 2>&1
				# 开始卸载
				color blue "begin to remove openssh"
				yum -y remove openssh-server openssh > /dev/null 2>&1
	else
			mv /usr/bin/ssh* /tmp/backup_$date/openssh/usr/bin > /dev/null 2>&1
			mv /usr/sbin/sshd /tmp/backup_$date/openssh/usr/sbin > /dev/null 2>&1
			mv /etc/init.d/sshd /tmp/backup_$date/openssh/etc/init.d > /dev/null 2>&1
			mv /etc/pam.d/sshd /tmp/backup_$date/openssh/etc/pam.d > /dev/null 2>&1
			mv /etc/ssh/ssh* /tmp/backup_$date/openssh/etc/ssh > /dev/null 2>&1
			mv /etc/ssh/sshd_config /tmp/backup_$date/openssh/etc/ssh > /dev/null 2>&1
			mv /usr/share/man/man1/ssh* /tmp/backup_$date/openssh/usr/share/man/man1 > /dev/null 2>&1
			mv /usr/share/man/man8/ssh* /tmp/backup_$date/openssh/usr/share/man/man8 > /dev/null 2>&1
			mv /usr/libexec/openssh/ssh* /tmp/backup_$date/openssh/usr/libexec/openssh > /dev/null 2>&1
	fi

}



